<?php
if (! defined ( 'BASEPATH' )) {
	exit ( 'no direct script access allowed!' );
}
/**
 * 好友推荐模块操作
 * Enter description here ...
 * @author 庆安
 *
 */
class theirwishes_model extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	function insertTheirwishes($data) {
		$this->db->insert ( "wanwan_enum_theirwishes", $data );
	}
	function insertTSContent($data) {
		$this->db->insert ( "wanwan_enum_twcontent", $data );
		return $this->db->insert_id ();
	}
	function getAllFriendBirthiday($userId,$limit=2) {
		$sql = "SELECT nickName,DATE_FORMAT(birthday,'%m月-%d日') birthday,userId,userBigIcon FROM wanwan_enum_user WHERE userid IN(SELECT userid FROM wanwan_enum_usertoteam WHERE teamid 
				IN(SELECT teamId FROM wanwan_enum_usertoteam WHERE userid=$userId) UNION ALL SELECT DISTINCT CASE WHEN userid=$userId THEN friendid ELSE userid END userid FROM
				wanwan_enum_usertouser WHERE userid=$userId OR friendid=$userId ) AND userid<>$userId AND  DATE_FORMAT(birthday,'%m-%d') BETWEEN DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL -30 DAY),'%m-%d') AND  DATE_FORMAT(NOW(),'%m-%d') ORDER BY DATE_FORMAT(birthday,'%m-%d')   LIMIT $limit";
		return $this->db->query ($sql)->result_array();
	}
	
	
	function addTheirwishes($data) {
		log_message ( "info", json_encode ( $data ) );
		$errorMsg = "";
		$successMsg = "";
		$data ['createTime'] = date ( 'Y-m-d H:i:s' );
		$data ['twId'] = $this->insertTSContent ( array ("createTime" => $data ["createTime"], "content" => $data ["twContent"], "delFlat" => 0 ) );
		$data = $this->__deleteKey ( "twContent", $data );
		$array = explode ( ",", $data ['accepterId'] );
		$referralCount = count ( $array );
		foreach ( $array as $val ) {
			$data ['accepterId'] = $val;
			$this->insertTheirwishes ( $data );
			$successMsg = "好友祝福推送成功";
		}
		return array ('msg' => $successMsg . $errorMsg . "." );
	}

	//Friendsreferral
}